package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DaoUtil {
	
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null; 
	
	DaoUtil(){
		getConnection();
	}
	
	
	//链接数据库
	public Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zgy","root","root");
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("connection success!");
		return null;
		
	}
	
	//关闭数据库
	public void closed(){
		
		try {
			if(rs!=null){
				rs.close();
				rs = null;
			}
			if(stat!=null){
				stat.close();
				stat = null;
			}
			if(conn!=null){
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
}
	
}
